﻿package mortal.game.view.common.item
{
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.util.*;
    import mortal.mvc.core.*;

    public class BaseItem extends CDItem
    {
        protected var _amount:int = 0;
        protected var _amountLabel:GTextFiled;
        protected var canNotUseEffect:ScaleBitmap;
        protected var _cannotUse:Boolean = false;
        protected var _isGray:Boolean;
        protected var lockedIcon:GBitmap;
        private var _isBind:Boolean = false;
        protected var _isShowLock:Boolean = true;
        protected var _itemData:ItemData;
        protected var _colorBg:ScaleBitmap;
        protected var _isShowColorBg:Boolean = true;
        protected var _isShowPrescritime:Boolean = true;
        protected var _qualityIcon:GImageBitmap;
        protected var _isShowQuality:Boolean = true;
        protected var _isShowStreng:Boolean = true;
        protected var _txtStrenLv:BitmapNumberText;
        protected var _isCheckOtherSkillBookState:Boolean = false;
        protected var _skillBookStateIcon:GImageBitmap;
        protected var _arroundEffect:SWFPlayer;
        protected var _isArround:Boolean;
        protected var _isShowArroundEffect:Boolean = true;
        protected var _label:GTextFiled;

        public function BaseItem()
        {
            return;
        }// end function

        public function get isSHowQuality() : Boolean
        {
            return this._isShowQuality;
        }// end function

        public function set isSHowQuality(param1:Boolean) : void
        {
            this._isShowQuality = param1;
            return;
        }// end function

        protected function resetQualitySize() : void
        {
            if (isDisposed)
            {
                return;
            }
            if (this._qualityIcon)
            {
                this._qualityIcon.width = _width - _paddingLeft * 2;
                this._qualityIcon.height = _height - _paddingTop * 2;
            }
            return;
        }// end function

        public function get isShowColorBg() : Boolean
        {
            return this._isShowColorBg;
        }// end function

        public function set isShowColorBg(param1:Boolean) : void
        {
            this._isShowColorBg = param1;
            return;
        }// end function

        override protected function updateView() : void
        {
            if (this.canNotUseEffect)
            {
                this.canNotUseEffect.width = _width;
                this.canNotUseEffect.height = _height;
            }
            if (this.lockedIcon)
            {
                this.lockedIcon.x = _paddingLeft;
                this.lockedIcon.y = _paddingTop;
            }
            if (this._amountLabel)
            {
                this._amountLabel.width = _width - _paddingLeft * 2;
                this._amountLabel.x = _paddingLeft;
                this._amountLabel.y = _height - _paddingTop - this._amountLabel.height;
            }
            if (this._label)
            {
                this._label.width = _width - _paddingLeft * 2;
                this._label.x = _paddingLeft + 3;
                this._label.y = _paddingTop - 3;
            }
            if (this._skillBookStateIcon)
            {
                this._skillBookStateIcon.x = (this.width - this._skillBookStateIcon.width) * 0.5;
                this._skillBookStateIcon.y = (this.height - this._skillBookStateIcon.height) * 0.5;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.configEventListener(MouseEvent.CLICK, this.showItem);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            if (this._amountLabel)
            {
                this._amountLabel.dispose(param1);
                this._amountLabel = null;
            }
            if (this.canNotUseEffect)
            {
                this.canNotUseEffect.dispose(param1);
                this.canNotUseEffect = null;
            }
            if (this._label)
            {
                this._label.dispose(param1);
                this._label = null;
            }
            if (this._txtStrenLv)
            {
                this._txtStrenLv.dispose(param1);
                this._txtStrenLv = null;
            }
            if (this.lockedIcon)
            {
                this.lockedIcon.dispose(param1);
                this.lockedIcon = null;
            }
            if (this._qualityIcon)
            {
                this._qualityIcon.dispose(param1);
                this._qualityIcon = null;
            }
            if (this._arroundEffect)
            {
                this._arroundEffect.dispose(param1);
                this._arroundEffect = null;
            }
            if (this._skillBookStateIcon)
            {
                this._skillBookStateIcon.dispose(param1);
                this._skillBookStateIcon = null;
            }
            this._amount = 0;
            this._isCheckOtherSkillBookState = false;
            this._cannotUse = false;
            this._isBind = false;
            this._isArround = false;
            this._isShowStreng = true;
            this._isShowLock = true;
            this._isShowColorBg = true;
            this._isShowQuality = true;
            this._isShowPrescritime = true;
            this._isShowArroundEffect = true;
            _paddingLeft = 2;
            _paddingTop = 2;
            this._itemData = null;
            if (this._colorBg != null)
            {
                this._colorBg.dispose(param1);
                this._colorBg = null;
            }
            super.disposeImpl(param1);
            return;
        }// end function

        public function set isPrescritime(param1:Boolean) : void
        {
            if (this._isShowPrescritime && param1)
            {
                this.labelText = !ItemsUtil.isPrescriOutTime(this._itemData) ? (Language.getString(20720)) : (Language.getString(20923));
            }
            else
            {
                this.labelText = "";
            }
            return;
        }// end function

        public function setLabelPos(param1:int, param2:int) : void
        {
            if (this._label)
            {
                this._label.x = param1;
                this._label.y = param2;
            }
            return;
        }// end function

        public function set labelText(param1:String) : void
        {
            var _loc_2:TextFormat = null;
            if (param1)
            {
                if (this._label == null)
                {
                    _loc_2 = GlobalStyle.textFormatBai.right();
                    this._label = UIFactory.gTextField("", _paddingLeft + 3, _paddingTop - 3, _width - _paddingLeft * 2, 20, _middleLayer, _loc_2);
                    this._label.mouseEnabled = false;
                }
                this._label.htmlText = param1;
            }
            else if (this._label)
            {
                this._label.dispose();
                this._label = null;
            }
            return;
        }// end function

        public function get labelText() : String
        {
            if (this._label)
            {
                return this._label.text;
            }
            return "";
        }// end function

        protected function showItem(event:MouseEvent) : void
        {
            if (event.ctrlKey && this.itemData && this.itemData.itemInfo)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatShowItem, this.itemData));
                event.stopImmediatePropagation();
            }
            return;
        }// end function

        override protected function onMouseDown(event:MouseEvent) : void
        {
            if (this.itemData && this.isDragAble && !this._cannotUse)
            {
                DragManager.instance.startDragItem(this, bitmapdata);
            }
            return;
        }// end function

        public function get isShowLock() : Boolean
        {
            return this._isShowLock;
        }// end function

        public function set isShowLock(param1:Boolean) : void
        {
            this._isShowLock = param1;
            return;
        }// end function

        public function setAmountLabelPos(param1:int, param2:int) : void
        {
            if (this._amountLabel)
            {
                this._amountLabel.x = param1;
                this._amountLabel.y = param2;
            }
            return;
        }// end function

        public function setAmountLabel(param1:String, param2:Boolean = true) : void
        {
            var _loc_3:TextFormat = null;
            if (param1)
            {
                if (this._amountLabel == null)
                {
                    _loc_3 = GlobalStyle.textFormatBai.right();
                    _loc_3.size = 11;
                    this.mouseChildren = false;
                    this._amountLabel = UIFactory.gTextField("", 0, 0, 100, 17, _middleLayer, _loc_3);
                    this._amountLabel.mouseEnabled = false;
                    this._amountLabel.autoSize = TextFieldAutoSize.NONE;
                }
                if (param2)
                {
                    this._amountLabel.htmlText = param1;
                }
                else
                {
                    this._amountLabel.text = param1;
                }
                this._amountLabel.width = _width - _paddingLeft * 2;
                this._amountLabel.x = _paddingLeft;
                this._amountLabel.y = _height - _paddingTop - this._amountLabel.height;
            }
            else if (this._amountLabel)
            {
                this._amountLabel.dispose();
                this._amountLabel = null;
            }
            return;
        }// end function

        public function get amount() : int
        {
            return this._amount;
        }// end function

        public function set amount(param1:int) : void
        {
            var _loc_2:String = null;
            if (this._amount != param1)
            {
                this._amount = param1;
                if (this._amount <= 1)
                {
                    _loc_2 = "";
                }
                else if (this._amount >= 100000)
                {
                    _loc_2 = Language.getStringByParam(20721, int(this._amount * 0.0001));
                }
                else
                {
                    _loc_2 = this._amount.toString();
                }
                this.setAmountLabel(_loc_2, false);
            }
            return;
        }// end function

        public function get canNotUse() : Boolean
        {
            return this._cannotUse;
        }// end function

        public function set canNotUse(param1:Boolean) : void
        {
            this._cannotUse = param1;
            if (this._cannotUse)
            {
                if (!this.canNotUseEffect)
                {
                    this.canNotUseEffect = ResourceConst.getScaleBitmap(ImagesConst.PackDisable2);
                    _middleLayer.addChild(this.canNotUseEffect);
                    this.canNotUseEffect.width = _width;
                    this.canNotUseEffect.height = _height;
                }
            }
            if (this.canNotUseEffect)
            {
                this.canNotUseEffect.visible = this._cannotUse;
            }
            return;
        }// end function

        override public function get dragSource() : Object
        {
            return this.itemData;
        }// end function

        override public function set dragSource(param1:Object) : void
        {
            super.dragSource = param1;
            if (param1 is ItemData)
            {
                this.itemData = param1 as ItemData;
            }
            return;
        }// end function

        public function get itemData() : ItemData
        {
            return this._itemData;
        }// end function

        public function set itemData(param1:ItemData) : void
        {
            var _loc_2:String = null;
            if (param1)
            {
                this._itemData = param1;
                if (param1.serverData.itemCode < 1000)
                {
                    this.source = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitBigIcon(param1.serverData.itemCode));
                    _loc_2 = GameDefConfig.instance.getEPrictUnitName(param1.serverData.itemCode);
                    if (this._itemData && this._itemData.serverData)
                    {
                        _loc_2 = _loc_2 + this._itemData.serverData.itemAmount;
                        this.amount = param1.serverData.itemAmount;
                    }
                    this.toolTipData = _loc_2;
                    this.isBind = false;
                    this.isPrescritime = false;
                    this.setQualiy(0);
                    this.updateQuality();
                    return;
                }
                else
                {
                    this.source = param1.itemInfo.url;
                    this.amount = param1.serverData.itemAmount;
                    this.isBind = ItemsUtil.isBind(this._itemData);
                    this.isPrescritime = ItemsUtil.isPrescriptionItem(param1);
                }
            }
            else
            {
                this.source = null;
                this.amount = 0;
                this._itemData = null;
                this.isBind = false;
                this.isPrescritime = false;
            }
            super.toolTipData = param1;
            this.updateColorShow();
            this.updateQuality();
            this.updateStrengLevel();
            this.updateAroundEffect();
            return;
        }// end function

        public function set isCheckOtherSkillBookState(param1:Boolean) : void
        {
            this._isCheckOtherSkillBookState = param1;
            this.checkMountSkillBookState();
            return;
        }// end function

        private function resetSkillBookStateIconPos() : void
        {
            if (this._skillBookStateIcon)
            {
                this._skillBookStateIcon.x = (this.width - this._skillBookStateIcon.width) * 0.5;
                this._skillBookStateIcon.y = (this.height - this._skillBookStateIcon.height) * 0.5;
            }
            return;
        }// end function

        public function checkMountSkillBookState() : void
        {
            var _loc_1:int = 0;
            if (this._isCheckOtherSkillBookState && this.itemData && this.itemData.itemInfo && (ItemsUtil.isOtherSkillBook(this.itemData) || ItemsUtil.isPetSkillBook(this.itemData)))
            {
                _loc_1 = MountUtil.getSkillBookStateByItem(this.itemData);
                if (_loc_1 == 0)
                {
                    if (this._skillBookStateIcon)
                    {
                        this._skillBookStateIcon.dispose();
                        this._skillBookStateIcon = null;
                    }
                }
                else
                {
                    if (this._skillBookStateIcon == null)
                    {
                        this._skillBookStateIcon = UIFactory.gImageBitmap("", 0, 0, _topLayer, this.resetSkillBookStateIconPos);
                    }
                    this._skillBookStateIcon.imgUrl = "otherSkillUpdateState_" + _loc_1 + ".png";
                }
            }
            else if (this._skillBookStateIcon)
            {
                this._skillBookStateIcon.dispose();
                this._skillBookStateIcon = null;
            }
            return;
        }// end function

        public function setQualiy(param1:int = 0, param2:String = "itemQualityIcon_") : void
        {
            if (param1 > 0)
            {
                if (this._qualityIcon == null)
                {
                    this._qualityIcon = UIFactory.gImageBitmap("", _paddingLeft, _paddingTop, _topLayer, this.resetQualitySize);
                }
                this._qualityIcon.imgUrl = param2 + param1 + ".png";
            }
            else if (this._qualityIcon)
            {
                this._qualityIcon.dispose();
                this._qualityIcon = null;
            }
            return;
        }// end function

        protected function updateQuality(param1:ItemData = null) : void
        {
            if (param1 == null)
            {
                param1 = this._itemData;
            }
            if (param1 == null)
            {
                this.setQualiy(0);
                return;
            }
            if (this._isShowQuality && param1.itemInfo != null)
            {
                this.setQualiy(param1.itemInfo.hunqiFeed);
            }
            else
            {
                this.setQualiy(0);
            }
            return;
        }// end function

        public function updateColorShow(param1:ItemData = null) : void
        {
            if (param1 == null)
            {
                param1 = this._itemData;
            }
            if (param1 == null)
            {
                this.updateColorBg(0);
                return;
            }
            if (this.isShowColorBg && param1.itemInfo != null)
            {
                this.updateColorBg(param1.itemInfo.color);
            }
            else
            {
                this.updateColorBg(0);
            }
            return;
        }// end function

        public function setColorBg(param1:int) : void
        {
            if (this.isShowColorBg)
            {
                this.updateColorBg(param1);
            }
            return;
        }// end function

        public function showEffect(param1:Boolean, param2:Number = 0, param3:Number = 0, param4:String = "ArroundEffect_zhi_small.swf") : void
        {
            if (param1)
            {
                if (this._arroundEffect == null)
                {
                    this._arroundEffect = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
                }
                this._arroundEffect.timeRate = 3;
                this._arroundEffect.move(param2, param3);
                this._arroundEffect.load(param4, ModelType.NormalSwf, null);
                this.addChild(this._arroundEffect);
            }
            else if (this._arroundEffect)
            {
                this._arroundEffect.dispose(true);
                this._arroundEffect = null;
            }
            return;
        }// end function

        public function updateAroundEffect() : void
        {
            if (this._itemData)
            {
                if (EquipmentUtil.isNeedAroundEffect(this._itemData) && this._isShowArroundEffect)
                {
                    if (!this._isArround)
                    {
                        this._isArround = true;
                        this.showEffect(true, 21 - 24, (21 - 1), "ArroundEffect.swf");
                    }
                }
                else
                {
                    this._isArround = false;
                    this.showEffect(false);
                }
            }
            else
            {
                this._isArround = false;
                this.showEffect(false);
            }
            return;
        }// end function

        public function clearArroundEffect() : void
        {
            this._isArround = false;
            if (this._arroundEffect)
            {
                this._arroundEffect.dispose();
                this._arroundEffect = null;
            }
            return;
        }// end function

        protected function updateColorBg(param1:int) : void
        {
            var _loc_2:String = null;
            if (param1 >= 1 && param1 <= 5)
            {
                _loc_2 = "ItemColorBg" + param1.toString();
                if (this._colorBg == null)
                {
                    this._colorBg = UIFactory.bg(1, 1, -1, -1, _middleLayer, _loc_2);
                }
                else
                {
                    this._colorBg.resetBitmapDataAndScale9Grid(GlobalClass.getBitmapData(_loc_2), ResourceConst.getRectangle(_loc_2));
                }
                _middleLayer.setChildIndex(this._colorBg, 0);
                this._colorBg.setSize(this.width - 2, this.height - 2);
            }
            else if (this._colorBg)
            {
                this._colorBg.dispose(true);
                this._colorBg = null;
            }
            return;
        }// end function

        public function set itemCode(param1:int) : void
        {
            if (param1 < 1000)
            {
                this.source = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitBigIcon(param1));
                this.toolTipData = GameDefConfig.instance.getEPrictUnitName(param1);
            }
            this.itemData = new ItemData(param1);
            return;
        }// end function

        public function set isBind(param1:Boolean) : void
        {
            this._isBind = param1;
            if (!this._isShowLock)
            {
                if (this.lockedIcon)
                {
                    this.lockedIcon.visible = false;
                }
                return;
            }
            if (this._isBind)
            {
                if (!this.lockedIcon)
                {
                    this.lockedIcon = GlobalClass.getBitmap(ImagesConst.LockIcon);
                    _middleLayer.addChild(this.lockedIcon);
                    this.lockedIcon.x = _paddingLeft;
                    this.lockedIcon.y = _paddingTop;
                }
            }
            if (this.lockedIcon)
            {
                this.lockedIcon.visible = this._isBind;
            }
            return;
        }// end function

        public function get isBind() : Boolean
        {
            return this._isBind;
        }// end function

        public function setItemStyle(param1:int = 36, param2:String = "PackItemBg", param3:int = 2, param4:int = 2) : void
        {
            _paddingTop = param3;
            _paddingLeft = param4;
            setSize(param1 + _paddingLeft * 2, param1 + _paddingTop * 2);
            this.bgName = param2;
            if (_bg)
            {
                _bg.width = _width;
                _bg.height = _height;
            }
            this.resetBitmapSize();
            return;
        }// end function

        override protected function resetBitmapSize() : void
        {
            if (_bitmap)
            {
                _bitmap.width = _width - _paddingLeft * 2;
                _bitmap.height = _height - _paddingTop * 2;
                _bitmap.x = _paddingLeft;
                _bitmap.y = _paddingTop;
            }
            return;
        }// end function

        public function set isGray(param1:Boolean) : void
        {
            this._isGray = param1;
            if (param1)
            {
                this.filters = [FilterConst.colorFilter2];
            }
            else
            {
                this.filters = [];
            }
            return;
        }// end function

        public function set isShowPrescritime(param1:Boolean) : void
        {
            this._isShowPrescritime = param1;
            return;
        }// end function

        public function updateStrengLevel(param1:int = 0) : void
        {
            var _loc_2:String = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this._isShowStreng)
            {
                _loc_2 = "";
                if (param1 != 0)
                {
                    _loc_2 = "+" + param1;
                }
                else if (this._itemData && this._itemData.itemInfo)
                {
                    if (ItemsUtil.isEquip(this._itemData) && this._itemData.extInfo)
                    {
                        _loc_3 = this._itemData.extInfo.strengthen;
                        _loc_4 = this._itemData.extInfo.currentStrengthen;
                        if (_loc_3 == 0 && _loc_4 == 0)
                        {
                            _loc_2 = "";
                        }
                        else
                        {
                            _loc_2 = "+" + _loc_3;
                        }
                    }
                    else if (ItemsUtil.isOtherEquip(this._itemData) && this._itemData.itemInfo.itemLevel)
                    {
                        _loc_2 = "+" + this._itemData.itemInfo.itemLevel;
                    }
                    else
                    {
                        _loc_2 = "";
                    }
                }
                if (_loc_2)
                {
                    if (this._txtStrenLv == null)
                    {
                        this._txtStrenLv = UIFactory.bitmapNumberText(0, 0, "FightInfoNum.png", 12, 15, -1, this);
                    }
                    this._txtStrenLv.text = _loc_2;
                    this._txtStrenLv.x = this.width - this._txtStrenLv.width - _paddingLeft;
                    this._txtStrenLv.y = this.height - this._txtStrenLv.height - 2 - _paddingTop;
                }
                else
                {
                    this.removeStrenLv();
                }
            }
            else
            {
                this.removeStrenLv();
            }
            return;
        }// end function

        public function removeStrenLv() : void
        {
            if (this._txtStrenLv)
            {
                this._txtStrenLv.dispose();
                this._txtStrenLv = null;
            }
            return;
        }// end function

        public function get isShowStreng() : Boolean
        {
            return this._isShowStreng;
        }// end function

        public function set isShowStreng(param1:Boolean) : void
        {
            this._isShowStreng = param1;
            return;
        }// end function

        public function get isShowArroundEffect() : Boolean
        {
            return this._isShowArroundEffect;
        }// end function

        public function set isShowArroundEffect(param1:Boolean) : void
        {
            this._isShowArroundEffect = param1;
            return;
        }// end function

    }
}
